﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Data;

[assembly: System.Web.UI.TagPrefix("Common.Controls", "cmnctr")]
[assembly: WebResource("Common.Controls.ImageGallery.ImageGallery.css", "css")]
namespace BaseSet.Entity.Common.Controls
{
    [ToolboxData("<{0}:ImageGallery runat=\"server\" />"), ValidationProperty("SelectedDate")]
    public sealed class ImageGallery : WebControl, IPostBackDataHandler
    {
        //[Description("获取或设置控件的宽度"), DefaultValue(typeof(Int32), "15")]
        //public int Width
        //{
        //    get
        //    {
        //        object objWidth = this.ViewState["Width"];
        //        return (objWidth == null) ? 0 : (Convert.ToInt32(objWidth));
        //    }
        //    set
        //    {
        //        this.ViewState["Width"] = value;
        //    }
        //}

        //[Description("获取或设置控件的宽度"), DefaultValue(typeof(Int32), "10")]
        //public int Height
        //{
        //    get
        //    {
        //        object objHeight = this.ViewState["Height"];
        //        return (objHeight == null) ? 0 : (Convert.ToInt32(objHeight));
        //    }
        //    set
        //    {
        //        this.ViewState["Height"] = value;
        //    }
        //}

        [Description("获取或设置控件的数据源")]
        protected Object DataSource
        {
            get
            {
                return this.ViewState["DataSource"];
            }
            set
            {
                this.ViewState["DataSource"] = value;
            }
        }

        [Description("获取或设置图形所在数据域")]
        protected string DataImageField
        {
            get
            {
                return this.ViewState["DataImageField"] + "";
            }
            set
            {
                this.ViewState["DataImageField"] = value;
            }
        }

        [Description("获取或设置超链接所在数据域")]
        protected string DataAnchorField
        {
            get
            {
                return this.ViewState["DataAnchorField"] + "";
            }
            set
            {
                this.ViewState["DataAnchorField"] = value;
            }
        }

        [Description("获取或设置图形数组")]
        public string[] ImageArray
        {
            get
            {
                return this.ViewState["ImageArray"] as string[];
            }
            set
            {
                this.ViewState["ImageArray"] = value;
            }
        }

        [Description("获取或设置超链接数组")]
        public string[] AnchorArray
        {
            get
            {
                return this.ViewState["AnchorArray"] as string[];
            }
            set
            {
                this.ViewState["AnchorArray"] = value;
            }
        }

        [Description("获取或设置超链接的打开方式")]
        public string AnchorTarget
        {
            get
            {
                return this.ViewState["AnchorArray"] + "";
            }
            set
            {
                this.ViewState["AnchorTarget"] = value;
            }
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ID))
            {
                throw new Exception("必需声明一个ID");
            }

            //if (this.ImageArray != null && this.AnchorArray != null)
            //{
            //    return;
            //}

            //if (this.ImageArray.Length != this.AnchorArray.Length)
            //{
            //    throw new Exception("图形数与超链接数 不匹配");
            //}

            base.OnPreRender(e);
            this.Page.RegisterRequiresPostBack(this);
            //if (this.Page != null)
            //{
            //    this.Page.ClientScript.RegisterClientScriptResource(base.GetType(), "Common.Controls.DatePicker.DataPicker.js");
            //}
            string id = "ImageGalleryCss";
            if (this.Page.Header.FindControl(id) == null)
            {
                HtmlLink child = new HtmlLink();
                child.ID = id;
                child.Href = this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "Common.Controls.ImageGallery.ImageGallery.css");
                child.Attributes["text"] = "text/css";
                child.Attributes["rel"] = "stylesheet";
                this.Page.Header.Controls.Add(child);
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.ImageArray.Length == 0 || this.AnchorArray.Length == 0)
            {
                return;
            }

            base.RenderContents(writer);

            //string str = this.ClientID + "_input";
            //string str2 = this.ClientID + "_div";
            //string clientclick = "show_calendar('" + str + "','" + str2 + "')";

            //writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "0px");
            //writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            //writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            
            //writer.RenderBeginTag(HtmlTextWriterTag.Table);
                //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                
                    //writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //this.RanderInput(writer, clientclick);
                    //writer.RenderEndTag();
                    
                    //writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //this.RanderImages(writer, clientclick);
                    //writer.RenderEndTag();
                
                //writer.RenderEndTag();
            //writer.RenderEndTag();

            //writer.AddAttribute(HtmlTextWriterAttribute.Id, str2);
            //writer.AddStyleAttribute(HtmlTextWriterStyle.Position, "absolute");

            //writer.RenderBeginTag(HtmlTextWriterTag.Div);
            //writer.RenderEndTag();

            //<div>
            //    <a href="#" id="url">
            //        <img src="#" id="pic" height="307" width="637">
            //    </a>
            //</div>

            //<div class="orderMenu">
            //    <a href="javascript:changeimg(1);" id="xxjdjj1" class="bxx" target="_self">1</a>
            //    <a href="javascript:changeimg(2);" id="xxjdjj2" class="axx" target="_self">2</a>
            //</div>

            // 轮换图形的容器ID
            string galleryId = this.ClientID + "_gallery";
            
            // 底部序号菜单ID
            string orderId = this.ClientID + "_order";

            #region DIV

            writer.AddAttribute(HtmlTextWriterAttribute.Id, galleryId);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            #region DIV_A

            writer.AddAttribute(HtmlTextWriterAttribute.Id, galleryId + "_a");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, this.AnchorArray[0]);
            writer.RenderBeginTag(HtmlTextWriterTag.A);

            #region DIV_A_IMG

            writer.AddAttribute(HtmlTextWriterAttribute.Id, galleryId + "_img");
            writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ImageArray[0]);
            writer.AddAttribute(HtmlTextWriterAttribute.Height, this.Height.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Width, this.Width.ToString());

            #endregion

            writer.RenderEndTag();

            #endregion

            writer.RenderEndTag();

            #endregion

            #region DIV2

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "orderMenu");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            #region DIV2_As

            for (int i = 0; i < this.ImageArray.Length; i++)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Format("{0}_a_{1}", orderId, i));
                writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("javascript:changeimg({0});", i));
                writer.AddAttribute(HtmlTextWriterAttribute.Class, i == 0 ? "bxx" : "axx");
                if (!string.IsNullOrEmpty(this.AnchorTarget))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Target, this.AnchorTarget);
                }
                else
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Target, "_self");
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Value, i + "");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.RenderEndTag();
            }

            #endregion

            writer.RenderEndTag();

            #endregion
        }

        #region IPostBackDataHandler 成员

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            throw new NotImplementedException();
        }

        public void RaisePostDataChangedEvent()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
